<?php include "include/function_library.php"; ?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php include "html/header.html"; ?>

</head>
<body class="tcuk home">

	<div id="wp">
	
		<?php  include "html/menu.html"; ?>
		
		<!-- CONTENT STARTS-->
		<div id="ct"> 	
			
			<?php 
			if(post("tp_submit")) {
				$tp_seat_number  = post("tp_seat_number");
				$tp_payment_type = post("tp_payment_type");
				$tp_user_id		 = session("id");
				$tp_date	 	 = date("Y-m-d");
				$tp_id		 	 = post("tp_id");
				
				$link = connect();
				$add_purchase_query = "INSERT INTO purchase (user_id, date, payment_type) VALUES ('$tp_user_id', '$tp_date', '$tp_payment_type')";
				
				$result_purchase = mysql_query($add_purchase_query, $link) or die("Sql Error! " . mysql_error());
				$last_added_purchase_id = mysql_insert_id();
				
				$query = "INSERT INTO transport_purchase (purchase_id, transportation_id, seat_number) VALUES ('$last_added_purchase_id', '$tp_id', '$tp_seat_number')";
				$result = $result_purchase && (mysql_query($query, $link) or die("Sql Error! " . mysql_error()));
				mysql_close($link);
				
				if($result) {
					redirect("transportation_detail.php?purchased=1&id=$tp_id");
				} else {
					echo "<div class='alert error'>Error! Could Not Purchase! There is(are) error(s) in the form!</div>";
				}

			} 
			
			$id = get("id");
			$query = "SELECT * FROM transportation WHERE id=$id";
			$result = sql_query($query);
			
			$tuple = mysql_fetch_assoc($result);
			?>
			<div style="width: 100%; text-align: center; margin-top:30px;">
				<h3>
					Your are purchasing <?php echo $tuple["type"]; ?> ticket 
					from <?php echo $tuple["source"]; ?> 
					to <?php echo $tuple["destination"]; ?> 
					with a price of <?php echo $tuple["price"]; ?>.
				</h3>
			</div>
			<form action="<?php echo curPageURL(); ?>?id=<?php echo $id ?>" method="post">
			<ul id="signup_form" style="width: 50%; margin-left: auto; margin-right: auto; margin-top: 30px; list-style: none;">
				<li>
					<label>Seat Number </label>
					<input type="number" id="tp_seat_number" name="tp_seat_number" placeholder="Seat number.." max="<?php echo $tuple["capacity"]; ?>" style="width: 250px;"/>
				</li>
				<li>
					<label>Payment Type </label>
					<select id="tp_payment_type" name="tp_payment_type" style="width: 265px;">
						<option value="Credit Card">Credit Card</option>
						<option value="Cash">Cash</option>
						<option value="Paypall">Paypall</option>
						<option value="Bitcoin">Bitcoin</option>
						<option value="EFT">EFT</option>
					</select>
				</li>
				<li>
					<input type="hidden" name="tp_id" value="<?php echo $id ?>"/>
					<input type="submit" id="tp_submit" name="tp_submit" value="Purchase" style="width: 90px; height:30px; margin-left: 80px;"/>
				</li>
			</ul>
			</form>
		</div> 	
		<!-- CONTENT ENDS-->		
		
		<?php include "html/footer.html"; ?>
		
	</div>
	
</body>
</html>